<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/img/icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/img/icon.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="cayun's blog, IT, ACM/ICPC, blog, cayun" />










<meta property="og:type" content="website">
<meta property="og:title" content="cayun&#39;s blog">
<meta property="og:url" content="http://localhost/index.html">
<meta property="og:site_name" content="cayun&#39;s blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="cayun&#39;s blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://localhost/"/>





  <title>cayun's blog</title>
  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-93052608-1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1ca7c9c8011b37b0c209724dc77c24db";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">cayun's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">不积跬步, 无以至千里!</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hello-world/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hello-world/" itemprop="url">Hello World</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-22T14:57:29+08:00">
                2017-10-22
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hello-world/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hello-world/" class="cy_cmt_count" data-xid="hello-world/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <p>Welcome to <a href="https://hexo.io/" target="_blank" rel="external">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="external">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="external">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="external">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="code"><pre><div class="line">$ hexo new <span class="string">"My New Post"</span></div></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/writing.html" target="_blank" rel="external">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="code"><pre><div class="line">$ hexo server</div></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/server.html" target="_blank" rel="external">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="code"><pre><div class="line">$ hexo generate</div></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/generating.html" target="_blank" rel="external">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="code"><pre><div class="line">$ hexo deploy</div></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/deployment.html" target="_blank" rel="external">Deployment</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/java_1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/java_1/" itemprop="url">Java获取外部程序输出结果</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-20T00:00:00+08:00">
                2017-10-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/java_1/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/java_1/" class="cy_cmt_count" data-xid="java_1/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          <p>今天遇到一个问题，需要对一个python程序的输出结果进行处理。<br>于是去网上搜了一下，可以通过jython来进行处理，但是但是问题来了，可能是python用的版本是3的原因，jython无法正常运作。<br>最终找到了Java与外部命令行程序的数据交互方式。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/java_1/" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu4018/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu4018/" itemprop="url">hdu 4018 Parsing URL (正则表达式)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-12T00:00:00+08:00">
                2017-09-12
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu4018/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu4018/" class="cy_cmt_count" data-xid="hdu4018/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p><code>(\\w*://)([a-z0-9-.]*)(.*)</code>第一部分匹配协议，第二部分匹配域名，最后一部分匹配后面的部分。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">import</span> java.util.Scanner;</div><div class="line"><span class="keyword">import</span> java.util.regex.Matcher;</div><div class="line"><span class="keyword">import</span> java.util.regex.Pattern;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</div><div class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String [] args)</span> </span>&#123;</div><div class="line">        Scanner sc = <span class="keyword">new</span> Scanner(System.in);</div><div class="line">    </div><div class="line">        <span class="keyword">int</span> t = sc.nextInt();</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">1</span>; kk &lt;= t; kk++) solve(kk, sc);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">solve</span><span class="params">(<span class="keyword">int</span> kk, Scanner sc)</span> </span>&#123;</div><div class="line">        String s = sc.next();</div><div class="line">        </div><div class="line">        String pattern = <span class="string">"(\\w*://)([a-z0-9-.]*)(.*)"</span>;</div><div class="line">        </div><div class="line">        Pattern r = Pattern.compile(pattern);</div><div class="line">        Matcher m = r.matcher(s);</div><div class="line">        </div><div class="line">        <span class="keyword">if</span> (m.find()) &#123;</div><div class="line">            System.out.printf(<span class="string">"Case #%d: %s"</span>, kk, m.group(<span class="number">2</span>));</div><div class="line">            System.out.println();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu1039/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu1039/" itemprop="url">hdu 1039 Easier Done Than Said? (正则表达式)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-12T00:00:00+08:00">
                2017-09-12
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu1039/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu1039/" class="cy_cmt_count" data-xid="hdu1039/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p><code>\\w*[aeiou]\\w*</code>表示匹配元音<br><code>\\w*([aeiou]{3}|[^aeiou]{3})\\w*</code>表示匹配连续3个元音或连续3个辅音<br><code>\\w*([^eo])\\1\\w*</code>表示匹配相邻相同的字母(除o、e外)</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">import</span> java.util.Scanner;</div><div class="line"><span class="keyword">import</span> java.util.regex.Pattern;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</div><div class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String [] args)</span> </span>&#123;</div><div class="line">        Scanner sc = <span class="keyword">new</span> Scanner(System.in);</div><div class="line">    </div><div class="line">        <span class="keyword">while</span> (sc.hasNext()) &#123;</div><div class="line">            String s = sc.next();</div><div class="line">            <span class="keyword">if</span> (<span class="string">"end"</span>.equals(s)) <span class="keyword">break</span>;</div><div class="line">            System.out.println(<span class="string">"&lt;"</span> + s + <span class="string">"&gt;"</span> + (solve(s) ? <span class="string">" is "</span> : <span class="string">" is not "</span>) + <span class="string">"acceptable."</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">solve</span><span class="params">(String s)</span> </span>&#123;</div><div class="line">        String pattern = <span class="string">"\\w*([aeiou]&#123;3&#125;|[^aeiou]&#123;3&#125;)\\w*"</span>;</div><div class="line">        <span class="keyword">if</span> (Pattern.matches(pattern, s)) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">        pattern = <span class="string">"\\w*[aeiou]\\w*"</span>;</div><div class="line">        <span class="keyword">if</span> (!Pattern.matches(pattern, s)) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">        pattern = <span class="string">"\\w*([^eo])\\1\\w*"</span>;</div><div class="line">        <span class="keyword">if</span> (Pattern.matches(pattern, s)) <span class="keyword">return</span> <span class="keyword">false</span>;    </div><div class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu6170/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu6170/" itemprop="url">hdu 6170 Two strings (记忆化搜索)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-23T00:00:00+08:00">
                2017-08-23
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu6170/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu6170/" class="cy_cmt_count" data-xid="hdu6170/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>直接暴力做匹配，用$dp_{i,j}$(A串匹配到第$i$位, B串匹配到第$j$位)存储已经计算过数据，避免重复计算。</p>
<p>提供一个小技巧。这一题对于B串，我们可以将其里面所有的*都删去，然后用一个数组来记录对应位是否有*。例如:<br>B: a*bb.*<br>可以转化为<br>B: abb.<br>C: 1001<br>其中C数组中的1就表示对应位有*。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">2510</span>;</div><div class="line"><span class="keyword">int</span> n, m;</div><div class="line"><span class="keyword">int</span> star[N];</div><div class="line"><span class="keyword">char</span> a[N], b[N], s[N];</div><div class="line"><span class="keyword">bool</span> f[N];</div><div class="line"><span class="keyword">bool</span> dp[N][N], vis[N][N];</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">bool</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> p, <span class="keyword">int</span> q)</span> </span>&#123;</div><div class="line">    <span class="keyword">if</span> (vis[p][q]) <span class="keyword">return</span> dp[p][q];</div><div class="line">    <span class="keyword">if</span> (p &gt; n &amp;&amp; q &gt; m) &#123;</div><div class="line">        vis[p][q] = <span class="literal">true</span>;</div><div class="line">        <span class="keyword">return</span> dp[p][q] = <span class="literal">true</span>; </div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span> (p &gt; n || q &gt; m) &#123;</div><div class="line">        vis[p][q] = <span class="literal">true</span>;</div><div class="line">        <span class="keyword">return</span> dp[p][q] = <span class="literal">false</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span> (b[q] != <span class="string">'.'</span> &amp;&amp; !f[q] &amp;&amp; a[p] != b[q]) &#123;</div><div class="line">        vis[p][q] = <span class="literal">true</span>;</div><div class="line">        <span class="keyword">return</span> dp[p][q] = <span class="literal">false</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span> (n - p &lt; m - q - star[q]) &#123;</div><div class="line">        vis[p][q] = <span class="literal">true</span>;</div><div class="line">        <span class="keyword">return</span> dp[p][q] = <span class="literal">false</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">if</span> (f[q]) &#123;</div><div class="line">        <span class="keyword">if</span> (b[q] == <span class="string">'.'</span>) &#123;</div><div class="line">            vis[p][q] = <span class="literal">true</span>;</div><div class="line">            <span class="keyword">if</span> (a[p] == a[p + <span class="number">1</span>]) <span class="keyword">return</span> dp[p][q] = dfs(p + <span class="number">1</span>, q + <span class="number">1</span>) || dfs(p, q + <span class="number">1</span>) || dfs(p + <span class="number">1</span>, q);</div><div class="line">            <span class="keyword">else</span> <span class="keyword">return</span> dp[p][q] = dfs(p + <span class="number">1</span>, q + <span class="number">1</span>) || dfs(p, q + <span class="number">1</span>);</div><div class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (a[p] == b[q]) &#123;</div><div class="line">            vis[p][q] = <span class="literal">true</span>;</div><div class="line">            <span class="keyword">return</span> dp[p][q] = dfs(p + <span class="number">1</span>, q + <span class="number">1</span>) || dfs(p, q + <span class="number">1</span>) || dfs(p + <span class="number">1</span>, q);</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            vis[p][q] = <span class="literal">true</span>;</div><div class="line">            <span class="keyword">return</span> dfs(p, q + <span class="number">1</span>);</div><div class="line">        &#125;</div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        <span class="keyword">if</span> (b[q] == <span class="string">'.'</span> || a[p] == b[q]) &#123;</div><div class="line">            vis[p][q] = <span class="literal">true</span>;</div><div class="line">            <span class="keyword">return</span> dp[p][q] = dfs(p + <span class="number">1</span>, q + <span class="number">1</span>);</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            vis[p][q] = <span class="literal">true</span>;</div><div class="line">            <span class="keyword">return</span> dp[p][q] = <span class="literal">false</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="built_in">memset</span>(f, <span class="literal">false</span>, <span class="keyword">sizeof</span> f);</div><div class="line">    <span class="built_in">memset</span>(vis, <span class="literal">false</span>, <span class="keyword">sizeof</span> vis);</div><div class="line">    <span class="built_in">scanf</span>(<span class="string">"%s%s"</span>, a + <span class="number">1</span>, s + <span class="number">1</span>);</div><div class="line">    n = <span class="built_in">strlen</span>(a + <span class="number">1</span>);</div><div class="line">    m = <span class="number">0</span>;</div><div class="line">    <span class="keyword">int</span> p = <span class="number">1</span>;</div><div class="line">    <span class="keyword">while</span> (s[p] != <span class="number">0</span>) &#123;</div><div class="line">        b[++m] = s[p++];</div><div class="line">        <span class="keyword">if</span> (s[p] == <span class="string">'*'</span>) &#123;</div><div class="line">            f[m] = <span class="literal">true</span>;</div><div class="line">            p++;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    star[m + <span class="number">1</span>] = <span class="number">0</span>;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = m; i &gt; <span class="number">0</span>; i--) &#123;</div><div class="line">        star[i] = star[i+<span class="number">1</span>];</div><div class="line">        <span class="keyword">if</span> (f[i]) star[i]++;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="built_in">puts</span>(dfs(<span class="number">1</span>, <span class="number">1</span>) ? <span class="string">"yes"</span> : <span class="string">"no"</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);</div><div class="line">    <span class="keyword">while</span> (T--) solve();</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu6165/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu6165/" itemprop="url">hdu 6165 FFF at Valentine (强连通分量+拓扑排序)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-23T00:00:00+08:00">
                2017-08-23
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu6165/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu6165/" class="cy_cmt_count" data-xid="hdu6165/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>首先使用强连通分量来进行缩点。缩点后该图变为了DAG图。然后做拓扑排序，若过程中出现了两个及以上的点入度同时为0则不合法。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1010</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> M = <span class="number">6010</span>;</div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; g[N], gg[N];</div><div class="line"><span class="keyword">int</span> low[N], dfn[N], belong[N], in[N];</div><div class="line"><span class="keyword">int</span> index, scc;</div><div class="line"><span class="keyword">bool</span> ins[N];</div><div class="line"><span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; stk;</div><div class="line"></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">tarjan</span><span class="params">(<span class="keyword">int</span> u)</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> v;</div><div class="line">    low[u] = dfn[u] = ++index;</div><div class="line">    stk.push(u);</div><div class="line">    ins[u] = <span class="literal">true</span>;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; g[u].size(); i++) &#123;</div><div class="line">        v = g[u][i];</div><div class="line">        <span class="keyword">if</span> (dfn[v] == <span class="number">0</span>) &#123;</div><div class="line">            tarjan(v);</div><div class="line">            <span class="keyword">if</span> (low[u] &gt; low[v]) low[u] = low[v];</div><div class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (ins[v] &amp;&amp; low[u] &gt; dfn[v]) &#123;</div><div class="line">            low[u] = dfn[v];</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span> (low[u] == dfn[u]) &#123;</div><div class="line">        scc++;</div><div class="line">        <span class="keyword">do</span> &#123;</div><div class="line">            v = stk.top(); stk.pop();</div><div class="line">            ins[v] = <span class="literal">false</span>;</div><div class="line">            belong[v] = scc;</div><div class="line">        &#125; <span class="keyword">while</span> (u != v);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">bool</span> <span class="title">solve</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> n, m; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;n, &amp;m);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= n; i++) &#123;</div><div class="line">        g[i].clear();</div><div class="line">        gg[i].clear();</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) &#123;</div><div class="line">        <span class="keyword">int</span> u, v; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;u, &amp;v);</div><div class="line">        g[u].push_back(v);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="built_in">memset</span>(dfn, <span class="number">0</span>, <span class="keyword">sizeof</span> dfn);</div><div class="line">    <span class="built_in">memset</span>(ins, <span class="literal">false</span>, <span class="keyword">sizeof</span> ins);</div><div class="line">    index = scc = <span class="number">0</span>;</div><div class="line">    <span class="keyword">while</span> (!stk.empty()) stk.pop();</div><div class="line"></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (dfn[i] == <span class="number">0</span>) tarjan(i);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="built_in">memset</span>(in, <span class="number">0</span>, <span class="keyword">sizeof</span> in);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; g[i].size(); j++) &#123;</div><div class="line">            <span class="keyword">int</span> v = g[i][j];</div><div class="line">            <span class="keyword">if</span> (belong[i] != belong[v]) &#123;</div><div class="line">                in[belong[v]]++;</div><div class="line">                gg[belong[i]].push_back(belong[v]);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; q;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= scc; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (in[i] == <span class="number">0</span>) &#123;</div><div class="line">            <span class="keyword">if</span> (!q.empty()) <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">            q.push(i);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">while</span> (!q.empty()) &#123;</div><div class="line">        <span class="keyword">int</span> u = q.front(); q.pop();</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; gg[u].size(); i++) &#123;</div><div class="line">            <span class="keyword">int</span> v = gg[u][i];</div><div class="line">            in[v]--;</div><div class="line">            <span class="keyword">if</span> (in[v] == <span class="number">0</span>) &#123;</div><div class="line">                <span class="keyword">if</span> (!q.empty()) <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">                q.push(v);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);</div><div class="line">    <span class="keyword">while</span> (T--) &#123;</div><div class="line">        solve() ? <span class="built_in">puts</span>(<span class="string">"I love you my love and our love save us!"</span>) : <span class="built_in">puts</span>(<span class="string">"Light my fire!"</span>);</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu3966/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu3966/" itemprop="url">hdu 3966 Aragorn's Story (树链剖分)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-23T00:00:00+08:00">
                2017-08-23
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu3966/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu3966/" class="cy_cmt_count" data-xid="hdu3966/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>树链剖分裸题。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> LL;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">50010</span>;</div><div class="line"><span class="keyword">int</span> a[N];</div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; g[N];</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">TreeCut</span> &#123;</span></div><div class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Node</span> &#123;</span></div><div class="line">        <span class="keyword">int</span> l, r;</div><div class="line">        LL val;</div><div class="line">    &#125;;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> total;</div><div class="line">    <span class="keyword">int</span> sz[N], dep[N], fat[N], son[N], belong[N], id[N], hs[N];</div><div class="line">    Node tree[N * <span class="number">4</span>];</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</div><div class="line">        sz[<span class="number">0</span>] = total = <span class="number">0</span>;</div><div class="line">        dfs1(<span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>);</div><div class="line">        dfs2(<span class="number">1</span>, <span class="number">1</span>);</div><div class="line">        buildTree(<span class="number">1</span>, <span class="number">0</span>, total);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// @private</span></div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">dfs1</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> father, <span class="keyword">int</span> depth)</span> </span>&#123;</div><div class="line">        sz[u] = <span class="number">1</span>;</div><div class="line">        fat[u] = father;</div><div class="line">        dep[u] = depth;</div><div class="line">        son[u] = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; g[u].size(); i++) &#123;</div><div class="line">            <span class="keyword">int</span> v = g[u][i];</div><div class="line">            <span class="keyword">if</span> (v != father) &#123;</div><div class="line">                sz[u] += dfs1(v, u, depth + <span class="number">1</span>);</div><div class="line">                <span class="keyword">if</span> (sz[v] &gt; sz[son[u]]) son[u] = v;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> sz[u];</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// @private</span></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">dfs2</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">        belong[u] = b;</div><div class="line">        id[u] = total;</div><div class="line">        hs[total++] = u;</div><div class="line">        <span class="keyword">if</span> (son[u]) dfs2(son[u], b);</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; g[u].size(); i++) &#123;</div><div class="line">            <span class="keyword">int</span> v = g[u][i];</div><div class="line">            <span class="keyword">if</span> (v != fat[u] &amp;&amp; v != son[u]) dfs2(v, v);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildTree</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>&#123;</div><div class="line">        tree[u].l = l; tree[u].r = r; tree[u].val = <span class="number">0</span>;</div><div class="line">        <span class="keyword">if</span> (l + <span class="number">1</span> == r) &#123;</div><div class="line">            tree[u].val = a[hs[l]];</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">int</span> mid = (l + r) &gt;&gt; <span class="number">1</span>, lch = u &lt;&lt; <span class="number">1</span>, rch = lch | <span class="number">1</span>;</div><div class="line">            buildTree(lch, l, mid);</div><div class="line">            buildTree(rch, mid, r);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(<span class="keyword">int</span> u)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> lch = u &lt;&lt; <span class="number">1</span>, rch = lch | <span class="number">1</span>;</div><div class="line">        tree[lch].val += tree[u].val;</div><div class="line">        tree[rch].val += tree[u].val;</div><div class="line">        tree[u].val = <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">modifyTree</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> l, <span class="keyword">int</span> r, LL x)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (l &lt;= tree[u].l &amp;&amp; tree[u].r &lt;= r) &#123;</div><div class="line">            tree[u].val += x;</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">if</span> (tree[u].val != <span class="number">0</span>) update(u);</div><div class="line">            <span class="keyword">int</span> mid = (tree[u].l + tree[u].r) &gt;&gt; <span class="number">1</span>, lch = u &lt;&lt; <span class="number">1</span>, rch  = lch | <span class="number">1</span>;</div><div class="line">            <span class="keyword">if</span> (l &lt; mid) modifyTree(lch, l, r, x);</div><div class="line">            <span class="keyword">if</span> (r &gt; mid) modifyTree(rch, l, r, x);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function">LL <span class="title">queryTree</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> p)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (tree[u].l + <span class="number">1</span> == tree[u].r) &#123;</div><div class="line">            <span class="keyword">return</span> tree[u].val;</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">if</span> (tree[u].val != <span class="number">0</span>) update(u);</div><div class="line">            <span class="keyword">int</span> mid = (tree[u].l + tree[u].r) &gt;&gt; <span class="number">1</span>, lch = u &lt;&lt; <span class="number">1</span>, rch = lch | <span class="number">1</span>;</div><div class="line">            <span class="keyword">return</span> p &lt; mid ? queryTree(lch, p) : queryTree(rch, p);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">modify</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y, LL k)</span> </span>&#123;</div><div class="line">        <span class="comment">//printf("x = %d, y = %d\n", x, y);</span></div><div class="line">        <span class="keyword">if</span> (belong[x] == belong[y]) &#123;</div><div class="line">            <span class="keyword">int</span> l = id[x], r = id[y];</div><div class="line">            <span class="keyword">if</span> (l &gt; r) swap(l, r);</div><div class="line">            modifyTree(<span class="number">1</span>, l, r + <span class="number">1</span>, k);</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">if</span> (dep[belong[x]] &lt; dep[belong[y]]) swap(x, y);</div><div class="line">            modifyTree(<span class="number">1</span>, id[belong[x]], id[x] + <span class="number">1</span>, k);</div><div class="line">            x = fat[belong[x]];</div><div class="line">            modify(x, y, k);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function">LL <span class="title">query</span><span class="params">(<span class="keyword">int</span> p)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> queryTree(<span class="number">1</span>, id[p]);</div><div class="line">    &#125;</div><div class="line">&#125;treeCut;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) g[i].clear();</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">(<span class="keyword">int</span> n, <span class="keyword">int</span> m, <span class="keyword">int</span> p)</span> </span>&#123;</div><div class="line">    init(n);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;a[i]);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= m; i++) &#123;</div><div class="line">        <span class="keyword">int</span> u, v; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;u, &amp;v);</div><div class="line">        g[u].push_back(v);</div><div class="line">        g[v].push_back(u);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    treeCut.init();</div><div class="line">    <span class="comment">//for (int i = 1; i &lt;= n; i++) cout &lt;&lt; treeCut.query(i) &lt;&lt; " "; cout &lt;&lt; endl;</span></div><div class="line">    <span class="keyword">while</span> (p--) &#123;</div><div class="line"></div><div class="line">        <span class="keyword">char</span> c; <span class="built_in">scanf</span>(<span class="string">" %c"</span>, &amp;c);</div><div class="line">        <span class="keyword">if</span> (c == <span class="string">'I'</span>) &#123;</div><div class="line">            <span class="keyword">int</span> x, y, k; <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>, &amp;x, &amp;y, &amp;k);</div><div class="line">            treeCut.modify(x, y, k);</div><div class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c == <span class="string">'D'</span>) &#123;</div><div class="line">            <span class="keyword">int</span> x, y, k; <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>, &amp;x, &amp;y, &amp;k);</div><div class="line">            treeCut.modify(x, y, -k);</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">int</span> x; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;x);</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, treeCut.query(x));</div><div class="line">        &#125;</div><div class="line">        <span class="comment">//for (int i = 1; i &lt;= n; i++) cout &lt;&lt; treeCut.query(i) &lt;&lt; " "; cout &lt;&lt; endl;</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> n, m, p;</div><div class="line">    <span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>, &amp;n, &amp;m, &amp;p) != EOF) solve(n, m, p);</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu6162/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu6162/" itemprop="url">hdu 6162 Ch’s gift (主席树)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-23T00:00:00+08:00">
                2017-08-23
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu6162/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu6162/" class="cy_cmt_count" data-xid="hdu6162/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>这道题就是SPOJ COT的区间查询版。</p>
<p>我们知道DFS实际上是维护一个前缀和。于是我们DFS遍历整棵树，然后在每个节点上建立线段树，每个线段树的前一版本是该节点的父节点上的线段树。</p>
<p>那么对于询问s,t，答案即为$root_s + root<em>t - root</em>{lca(s, t)} - root_{fat(lca(s, t))}$上的区间询问。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> LL;</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> &#123;</span></div><div class="line">    <span class="keyword">int</span> lch, rch;</div><div class="line">    LL sum;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100010</span>;</div><div class="line"><span class="keyword">int</span> hn, total;</div><div class="line"><span class="keyword">int</span> c[N], hs[N], fat[N], root[N];</div><div class="line">Node tree[N * <span class="number">20</span>];</div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; g[N];</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">ST</span> &#123;</span></div><div class="line">    <span class="keyword">enum</span> &#123;ST_SIZE = <span class="number">20</span>&#125;;</div><div class="line">    <span class="keyword">int</span> logTwo[N &lt;&lt; <span class="number">1</span>], st[N &lt;&lt; <span class="number">1</span>][ST_SIZE];</div><div class="line"></div><div class="line">    ST() &#123;</div><div class="line">        logTwo[<span class="number">0</span>] = <span class="number">-1</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; (N &lt;&lt; <span class="number">1</span>); i++) &#123;</div><div class="line">            logTwo[i] = ((i &amp; (i - <span class="number">1</span>)) == <span class="number">0</span>) ? logTwo[i<span class="number">-1</span>] + <span class="number">1</span> : logTwo[i<span class="number">-1</span>];</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> *a, <span class="keyword">int</span> n)</span> </span>&#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) st[i][<span class="number">0</span>] = a[i];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; ST_SIZE; j++)</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i + (<span class="number">1</span> &lt;&lt; j) &lt;= n + <span class="number">1</span>; i++) </div><div class="line">            st[i][j] = min(st[i][j<span class="number">-1</span>], st[i + (<span class="number">1</span> &lt;&lt; (j<span class="number">-1</span>))][j<span class="number">-1</span>]);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> k = logTwo[r-l+<span class="number">1</span>];</div><div class="line">        <span class="keyword">return</span> min(st[l][k], st[r+<span class="number">1</span>-(<span class="number">1</span>&lt;&lt;k)][k]);</div><div class="line">    &#125;</div><div class="line">&#125;st;</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">LCA</span> &#123;</span></div><div class="line">    <span class="keyword">int</span> p, m;</div><div class="line">    <span class="keyword">int</span> mp[N &lt;&lt; <span class="number">1</span>], val[N &lt;&lt; <span class="number">1</span>], first[N];</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</div><div class="line">        p = m = <span class="number">0</span>;</div><div class="line">        dfs(<span class="number">1</span>, <span class="number">0</span>);</div><div class="line">        st.init(val, m);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> f)</span> </span>&#123;</div><div class="line">        mp[++p] = u;</div><div class="line">        val[++m] = p;</div><div class="line">        first[u] = m;</div><div class="line">        <span class="keyword">int</span> t = p;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; g[u].size(); i++) &#123;</div><div class="line">            <span class="keyword">int</span> v = g[u][i];</div><div class="line">            <span class="keyword">if</span> (v == f) <span class="keyword">continue</span>;</div><div class="line">            dfs(v, u);</div><div class="line">            val[++m] = t;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (first[a] &gt; first[b]) swap(a, b);</div><div class="line">        <span class="keyword">return</span> mp[st.query(first[a], first[b])];</div><div class="line">    &#125;</div><div class="line">&#125;lca;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">insertTree</span><span class="params">(<span class="keyword">int</span> &amp;p, <span class="keyword">int</span> l, <span class="keyword">int</span> r, <span class="keyword">int</span> x)</span> </span>&#123;</div><div class="line">    tree[++total] = tree[p];</div><div class="line">    p = total;</div><div class="line">    tree[p].sum += hs[x];</div><div class="line">    <span class="keyword">if</span> (l + <span class="number">1</span> == r) <span class="keyword">return</span>;</div><div class="line">    <span class="keyword">int</span> mid = (l + r) &gt;&gt; <span class="number">1</span>;</div><div class="line">    <span class="keyword">if</span> (x &lt; mid) insertTree(tree[p].lch, l, mid, x);</div><div class="line">    <span class="keyword">else</span> insertTree(tree[p].rch, mid, r, x);</div><div class="line">&#125; </div><div class="line"></div><div class="line"><span class="function">LL <span class="title">queryTree</span><span class="params">(<span class="keyword">int</span> rs, <span class="keyword">int</span> rt, <span class="keyword">int</span> rr, <span class="keyword">int</span> rf, <span class="keyword">int</span> l, <span class="keyword">int</span> r, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">    <span class="comment">//printf("l=%d, r=%d, a=%d, b=%d\n", l, r, a, b);</span></div><div class="line">    <span class="keyword">if</span> (a &lt;= l &amp;&amp; r &lt;= b) &#123;</div><div class="line">        <span class="comment">//printf("sum: %lld %lld %lld %lld\n", tree[rs].sum, tree[rt].sum, tree[rr].sum, tree[rf].sum);</span></div><div class="line">        <span class="keyword">return</span> tree[rs].sum + tree[rt].sum - tree[rr].sum - tree[rf].sum;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">int</span> mid = (l + r) &gt;&gt; <span class="number">1</span>;</div><div class="line">    LL ans = <span class="number">0</span>;</div><div class="line">    <span class="keyword">if</span> (a &lt; mid &amp;&amp; l &lt; b) ans += queryTree(tree[rs].lch, tree[rt].lch, tree[rr].lch, tree[rf].lch, l, mid, a, b);</div><div class="line">    <span class="keyword">if</span> (a &lt; r &amp;&amp; mid &lt; b) ans += queryTree(tree[rs].rch, tree[rt].rch, tree[rr].rch, tree[rf].rch, mid, r, a, b);</div><div class="line">    <span class="keyword">return</span> ans;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> u)</span> </span>&#123;</div><div class="line">    root[u] = root[fat[u]];</div><div class="line">    insertTree(root[u], <span class="number">1</span>, hn, c[u]);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; g[u].size(); i++) &#123;</div><div class="line">        <span class="keyword">int</span> v = g[u][i];</div><div class="line">        <span class="keyword">if</span> (v != fat[u]) &#123;</div><div class="line">            fat[v] = u;</div><div class="line">            dfs(v);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">(<span class="keyword">int</span> n, <span class="keyword">int</span> m)</span> </span>&#123;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;c[i]);</div><div class="line">        hs[i] = c[i];</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) g[i].clear();</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</div><div class="line">        <span class="keyword">int</span> u, v; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;u, &amp;v);</div><div class="line">        g[u].push_back(v);</div><div class="line">        g[v].push_back(u);</div><div class="line">    &#125;</div><div class="line">    sort(hs + <span class="number">1</span>, hs + n + <span class="number">1</span>);</div><div class="line">    hn = unique(hs + <span class="number">1</span>, hs + n + <span class="number">1</span>) - hs;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) c[i] = lower_bound(hs + <span class="number">1</span>, hs + hn, c[i]) - hs;</div><div class="line"></div><div class="line">    lca.init();</div><div class="line"></div><div class="line">    root[<span class="number">0</span>] = total = tree[<span class="number">0</span>].lch = tree[<span class="number">0</span>].rch = tree[<span class="number">0</span>].sum = <span class="number">0</span>;</div><div class="line">    fat[<span class="number">1</span>] = <span class="number">0</span>;</div><div class="line">    dfs(<span class="number">1</span>);</div><div class="line"></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= m; i++) &#123;</div><div class="line">        <span class="keyword">int</span> s, t, a, b;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d%d%d%d"</span>, &amp;s, &amp;t, &amp;a, &amp;b);</div><div class="line">        a = lower_bound(hs + <span class="number">1</span>, hs + hn, a) - hs;</div><div class="line">        b = upper_bound(hs + <span class="number">1</span>, hs + hn, b) - hs;</div><div class="line">        <span class="keyword">int</span> r = lca.query(s, t);</div><div class="line">        <span class="comment">//printf("s=%d, t=%d, r=%d, fat[r]=%d\n", s, t, r, fat[r]);</span></div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%lld"</span>, queryTree(root[s], root[t], root[r], root[fat[r]], <span class="number">1</span>, hn, a, b));</div><div class="line">        <span class="keyword">if</span> (i == m) <span class="built_in">printf</span>(<span class="string">"\n"</span>);</div><div class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">" "</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> n, m;</div><div class="line">    <span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;n, &amp;m) != EOF) solve(n, m);</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu6096/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu6096/" itemprop="url">hdu 6096 String (AC自动机)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-12T00:00:00+08:00">
                2017-08-12
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu6096/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu6096/" class="cy_cmt_count" data-xid="hdu6096/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>解法见<a href="http://www.cnblogs.com/forever97/p/hdu6096.html" target="_blank" rel="external">forever97的博客</a></p>
<p>主要就是将模式串以[后缀#前缀]的形式插入AC自动机中。然后匹配时将原串以[原串#原串]的形式去进行匹配。因为中间有字符’#’，所以不会出现多次同一模式串多次匹配的情况。最后通过fail链来累加计数就可以了。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> NDEBUG</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">ACAutoMachine</span> &#123;</span></div><div class="line">    <span class="keyword">enum</span> &#123;NODE_NUM = <span class="number">1200010</span>, CHARSER_NUM = <span class="number">27</span>, BASE = <span class="string">'a'</span>&#125;;</div><div class="line">    <span class="keyword">int</span> total;</div><div class="line">    <span class="keyword">int</span> trie[NODE_NUM][CHARSER_NUM], fail[NODE_NUM], num[NODE_NUM], dep[NODE_NUM];</div><div class="line">    <span class="keyword">int</span> ep[NODE_NUM], link[NODE_NUM];</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</div><div class="line">        total = <span class="number">0</span>;</div><div class="line">        <span class="built_in">memset</span>(trie, <span class="number">-1</span>, <span class="keyword">sizeof</span> trie);</div><div class="line">        <span class="built_in">memset</span>(fail, <span class="number">-1</span>, <span class="keyword">sizeof</span> fail);</div><div class="line">        <span class="built_in">memset</span>(dep, <span class="number">0</span>, <span class="keyword">sizeof</span> dep);</div><div class="line">        <span class="built_in">memset</span>(ep, <span class="number">0</span>, <span class="keyword">sizeof</span> ep);</div><div class="line">        <span class="built_in">memset</span>(num, <span class="number">0</span>, <span class="keyword">sizeof</span> num);</div><div class="line">        <span class="built_in">memset</span>(link, <span class="number">0</span>, <span class="keyword">sizeof</span> link);</div><div class="line">        fail[<span class="number">0</span>] = <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">insert</span><span class="params">(<span class="keyword">char</span> s[])</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> p = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; s[i] != <span class="number">0</span>; i++) &#123;</div><div class="line">            <span class="keyword">int</span> ch = s[i] - BASE;</div><div class="line">            <span class="keyword">if</span> (trie[p][ch] == <span class="number">-1</span>) &#123;</div><div class="line">                trie[p][ch] = ++total;</div><div class="line">                dep[total] = i + <span class="number">1</span>;</div><div class="line">            &#125;</div><div class="line">            p = trie[p][ch];</div><div class="line">        &#125;</div><div class="line">        ep[p]++;</div><div class="line">        assert(p &gt; <span class="number">0</span> &amp;&amp; p &lt; NODE_NUM);</div><div class="line">        <span class="keyword">return</span> p;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">build</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; CHARSER_NUM; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (trie[<span class="number">0</span>][i] == <span class="number">-1</span>) trie[<span class="number">0</span>][i] = <span class="number">0</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; q;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; CHARSER_NUM; i++) &#123;</div><div class="line">            <span class="keyword">int</span> tmp = trie[<span class="number">0</span>][i];</div><div class="line">            assert(tmp &gt;= <span class="number">0</span>);</div><div class="line">            <span class="keyword">if</span> (tmp != <span class="number">0</span>) &#123;</div><div class="line">                fail[tmp] = <span class="number">0</span>;</div><div class="line">                q.push(tmp);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">while</span> (!q.empty()) &#123;</div><div class="line">            <span class="keyword">int</span> u = q.front(); q.pop();</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; CHARSER_NUM; i++) &#123;</div><div class="line">                <span class="keyword">int</span> tmp = trie[u][i];</div><div class="line">                assert(tmp &gt;= <span class="number">-1</span> &amp;&amp; tmp &lt; NODE_NUM);</div><div class="line">                <span class="keyword">if</span> (tmp != <span class="number">-1</span>) &#123;</div><div class="line">                    <span class="keyword">int</span> f = fail[u];</div><div class="line">                    <span class="keyword">while</span> (trie[f][i] == <span class="number">-1</span>) f = fail[f];</div><div class="line">                    f = trie[f][i];</div><div class="line">                    assert(f &gt;= <span class="number">0</span>);</div><div class="line">                    fail[tmp] = f;</div><div class="line">                    ep[tmp] += ep[f];</div><div class="line">                    q.push(tmp); </div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">findNextState</span><span class="params">(<span class="keyword">int</span> p, <span class="keyword">char</span> c)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> ch = c - BASE;</div><div class="line">        <span class="keyword">while</span> (trie[p][ch] == <span class="number">-1</span>) p = fail[p];</div><div class="line">        <span class="keyword">return</span> trie[p][ch];</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">searchWords</span><span class="params">(<span class="keyword">char</span> text[], <span class="keyword">int</span> len)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;</div><div class="line">        <span class="keyword">int</span> p = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; text[i] != <span class="number">0</span>; i++) &#123;</div><div class="line">            p = findNextState(p, text[i]);</div><div class="line">            assert(p &gt;= <span class="number">0</span> &amp;&amp; p &lt; NODE_NUM);</div><div class="line">            <span class="keyword">while</span> (dep[p] &gt; len) p = fail[p];</div><div class="line">            assert(p &gt;= <span class="number">0</span> &amp;&amp; p &lt; NODE_NUM);</div><div class="line">            <span class="keyword">if</span> (ep[p] != <span class="number">0</span>) num[p]++;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">getAns</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; q;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= total; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (fail[i] != <span class="number">0</span>) link[fail[i]]++;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= total; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (link[i] == <span class="number">0</span>) q.push(i);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">while</span> (!q.empty()) &#123;</div><div class="line">            <span class="keyword">int</span> u = q.front(); q.pop();</div><div class="line">            <span class="keyword">int</span> f = fail[u];</div><div class="line">            <span class="keyword">if</span> (f == <span class="number">0</span>) <span class="keyword">continue</span>;</div><div class="line">            num[f] += num[u];</div><div class="line">            <span class="keyword">if</span> (--link[f] == <span class="number">0</span>) q.push(f);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;acam;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100010</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> LEN = <span class="number">500010</span>;</div><div class="line"><span class="keyword">char</span> str[LEN], s[N &lt;&lt; <span class="number">1</span>], t[N &lt;&lt; <span class="number">1</span>];</div><div class="line"><span class="keyword">int</span> lft[N], rgt[N], pt[N];</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> n, q; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;n, &amp;q);</div><div class="line">    rgt[<span class="number">0</span>] = <span class="number">0</span>;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        lft[i] = rgt[i<span class="number">-1</span>];</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%s"</span>, str + lft[i]);</div><div class="line">        rgt[i] = <span class="built_in">strlen</span>(str + lft[i]) + lft[i];</div><div class="line">    &#125;</div><div class="line">    acam.init();</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= q; i++) &#123;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%s%s"</span>, s, t);</div><div class="line">        <span class="built_in">strcat</span>(t, <span class="string">"&#123;"</span>);</div><div class="line">        <span class="built_in">strcat</span>(t, s);</div><div class="line">        <span class="comment">//cerr &lt;&lt; t &lt;&lt; endl;</span></div><div class="line">        pt[i] = acam.insert(t);</div><div class="line">    &#125;</div><div class="line">    acam.build();</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        <span class="built_in">strncpy</span>(s, str + lft[i], rgt[i] - lft[i]);</div><div class="line">        <span class="keyword">int</span> p = rgt[i] - lft[i];</div><div class="line">        s[p++] = <span class="string">'&#123;'</span>;</div><div class="line">        <span class="built_in">strncpy</span>(s + p, str + lft[i], rgt[i] - lft[i]);</div><div class="line">        p += rgt[i] - lft[i];</div><div class="line">        s[p++] = <span class="number">0</span>;</div><div class="line"></div><div class="line">        <span class="comment">//cerr &lt;&lt; s &lt;&lt; endl;</span></div><div class="line">        acam.searchWords(s, rgt[i] - lft[i] + <span class="number">1</span>);</div><div class="line">    &#125;</div><div class="line">    acam.getAns();</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= q; i++) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, acam.num[pt[i]]);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);</div><div class="line">    <span class="keyword">while</span> (T--) solve();</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu6093/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu6093/" itemprop="url">hdu 6093 Rikka with Number (数位DP)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-08T00:00:00+08:00">
                2017-08-08
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu6093/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu6093/" class="cy_cmt_count" data-xid="hdu6093/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>一道数位DP的裸题，写起来有点麻烦。</p>
<p>首先转化为计算不大于$n$的好数有多少个。因为$[x^{x-1}, x^x)$与$[(x+1)^x,(x+1)^{x+1})$是不相交的，所以任意一个好数都只会落在一个区间中。</p>
<p>计算可以得出$x$进制下的好数个数为$(x-1) \times (x-1)!$。那么对于一个数$n$，假设其落在$[x^{x-1}, x^x)$中，那么我们可以预处理出前面$1…x-1$部分的答案。然后第$x$部分额外处理。这很显然就是一个数位DP。</p>
<p>至于$n$落在哪个区间中，我们可以二分求得。那么最终答案即为$f(R) - f(L-1)$。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">import</span> java.io.*;</div><div class="line"><span class="keyword">import</span> java.math.*;</div><div class="line"><span class="keyword">import</span> java.util.*;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</div><div class="line">    <span class="keyword">static</span> <span class="keyword">long</span> MOD;</div><div class="line">    <span class="keyword">static</span> BigInteger [] xx;</div><div class="line">    <span class="keyword">static</span> BigInteger [] xm;</div><div class="line">    <span class="keyword">static</span> <span class="keyword">long</span> [] fac;</div><div class="line">    <span class="keyword">static</span> <span class="keyword">long</span> [] sum;</div><div class="line">    <span class="keyword">static</span> <span class="keyword">boolean</span> [] vis;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String [] args)</span> </span>&#123;</div><div class="line">        init();</div><div class="line">        Scanner sc = <span class="keyword">new</span> Scanner(System.in);</div><div class="line">        <span class="keyword">int</span> T = sc.nextInt();</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">0</span>; kk &lt; T; kk++) &#123;</div><div class="line">            BigInteger l = sc.nextBigInteger();</div><div class="line">            BigInteger r = sc.nextBigInteger();</div><div class="line">            <span class="keyword">long</span> ans = solve(r) - solve(l.subtract(BigInteger.ONE));</div><div class="line">            ans %= MOD;</div><div class="line">            <span class="keyword">if</span> (ans &lt; <span class="number">0</span>) ans += MOD;</div><div class="line">            System.out.println(ans);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</div><div class="line">        MOD = <span class="number">998244353</span>;</div><div class="line"></div><div class="line">        fac = <span class="keyword">new</span> <span class="keyword">long</span>[<span class="number">2000</span>];</div><div class="line">        sum = <span class="keyword">new</span> <span class="keyword">long</span>[<span class="number">2000</span>];</div><div class="line"></div><div class="line">        fac[<span class="number">0</span>] = <span class="number">1</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; <span class="number">2000</span>; i++) &#123;</div><div class="line">            fac[i] = fac[i-<span class="number">1</span>] * i % MOD;</div><div class="line">            sum[i] = (i - <span class="number">1</span>) * fac[i-<span class="number">1</span>] % MOD;</div><div class="line">            <span class="keyword">if</span> (i &gt; <span class="number">2</span>) &#123;</div><div class="line">                sum[i] += sum[i-<span class="number">1</span>];</div><div class="line">                <span class="keyword">if</span> (sum[i] &gt;= MOD) sum[i] -= MOD;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        xx = <span class="keyword">new</span> BigInteger[<span class="number">2000</span>];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; <span class="number">2000</span>; i++) &#123;</div><div class="line">            xx[i] = BigInteger.valueOf(i).pow(i);</div><div class="line">        &#125;</div><div class="line">        xm = <span class="keyword">new</span> BigInteger[<span class="number">2000</span>];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; <span class="number">2000</span>; i++) &#123;</div><div class="line">            xm[i] = BigInteger.valueOf(i).pow(i-<span class="number">1</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">int</span> <span class="title">myBinary</span><span class="params">(BigInteger x, <span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;</div><div class="line">        <span class="keyword">while</span> (l &lt;= r) &#123;</div><div class="line">            <span class="keyword">int</span> mid = (l + r) &gt;&gt; <span class="number">1</span>;</div><div class="line">            <span class="keyword">if</span> (x.compareTo(xm[mid]) &gt;= <span class="number">0</span>) &#123;</div><div class="line">                ans = mid;</div><div class="line">                l = mid + <span class="number">1</span>;</div><div class="line">            &#125; <span class="keyword">else</span> &#123;</div><div class="line">                r = mid - <span class="number">1</span>;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">long</span> <span class="title">solve</span><span class="params">(BigInteger n)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (n.compareTo(BigInteger.valueOf(<span class="number">2</span>)) &lt; <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">        <span class="keyword">int</span> t = myBinary(n, <span class="number">2</span>, <span class="number">1999</span>);</div><div class="line">        <span class="comment">//System.out.println(n + " " + t);</span></div><div class="line">        <span class="keyword">if</span> (n.compareTo(xx[t]) &gt;= <span class="number">0</span>) <span class="keyword">return</span> sum[t];</div><div class="line">        <span class="keyword">long</span> ans = sum[t - <span class="number">1</span>];</div><div class="line">        <span class="keyword">long</span> res = <span class="number">0</span>;</div><div class="line">        vis = <span class="keyword">new</span> <span class="keyword">boolean</span>[t];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; t; i++) vis[i] = <span class="keyword">false</span>;</div><div class="line">        BigInteger [] tn = <span class="keyword">new</span> BigInteger[t];</div><div class="line">        tn[<span class="number">0</span>] = BigInteger.ONE;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; t; i++) &#123;</div><div class="line">            tn[i] = tn[i-<span class="number">1</span>].multiply(BigInteger.valueOf(t));</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = t - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123;</div><div class="line">            <span class="keyword">int</span> x = (<span class="keyword">int</span>)(n.divide(tn[i]).longValue());</div><div class="line">            <span class="keyword">if</span> (i == t - <span class="number">1</span>) &#123;</div><div class="line">                res = (res + (x - <span class="number">1</span>) * fac[t-<span class="number">1</span>]) % MOD;</div><div class="line">                vis[x] = <span class="keyword">true</span>;</div><div class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (vis[x]) &#123;</div><div class="line">                <span class="keyword">int</span> cnt = <span class="number">0</span>;</div><div class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; x; j++) &#123;</div><div class="line">                    <span class="keyword">if</span> (!vis[j]) cnt++;</div><div class="line">                &#125;</div><div class="line">                res = (res + cnt * fac[i]) % MOD;</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">            &#125; <span class="keyword">else</span> &#123;</div><div class="line">                <span class="keyword">int</span> cnt = <span class="number">0</span>;</div><div class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; x; j++) &#123;</div><div class="line">                    <span class="keyword">if</span> (!vis[j]) cnt++;</div><div class="line">                &#125;</div><div class="line">                res = (res + cnt * fac[i]) % MOD;</div><div class="line">                vis[x] = <span class="keyword">true</span>;</div><div class="line">                <span class="keyword">if</span> (i == <span class="number">0</span>) &#123;</div><div class="line">                    res++;</div><div class="line">                    <span class="keyword">if</span> (res &gt;= MOD) res -= MOD;</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">            n = n.subtract(tn[i].multiply(BigInteger.valueOf(x)));</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> (ans + res) % MOD;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><span class="space">&hellip;</span><a class="page-number" href="/page/11/">11</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/img/head.png"
                alt="cayun" />
            
              <p class="site-author-name" itemprop="name">cayun</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">102</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">79</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/comeacrossyun" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:comeacrossyun@gmail.com" target="_blank" title="E-Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">cayun</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a></div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  




  
    <script id="cy_cmt_num" src="https://changyan.sohu.com/upload/plugins/plugins.list.count.js?clientId=cyt6VLWhj"></script>
  









  





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
